<?php  
class User extends MY_Controller {  
	private $error = array();
   
  	public function index() {
    	$this->load->language('user/user');

    	$this->document->setTitle($this->lang->line('heading_title'));
		
		$script = base_url()."includes/js/easyui/jquery.easyui.min.js";
		$this->document->addScript($script);
		
		$style_one = base_url()."includes/js/easyui/themes/default/easyui.css";
		$style_two = base_url()."includes/js/easyui/themes/icon.css";
		
		$this->document->addStyle($style_one);
		$this->document->addStyle($style_two);
		
		$this->load->model('setting/Model_Config');
	
		$this->load->model('user/Model_User');
		
    	$this->getList();
  	}
	
	function datagrid()
	{
    	$this->load->language('user/user');	
		$this->load->model('setting/Model_Config');
		$this->load->model('catalog/Model_User');
		
		
		if($this->input->post('page'))
		{
			$page = (int)$this->input->post('page');
			$rows = (int)$this->input->post('rows');
		}
		else
		{
			$page = 1;
			$rows = 10;
		}
		
		$array_one = array();
		
			$sort = $this->input->post('sort');
			
			switch($sort)
			{
				case "user_id":				
					$sortone = "u.user_id ".$this->input->post('order');
					break;
				case "username":
					$sortone = "u.username ".$this->input->post('order');
					break;
				case "status":
					$sortone = "u.status ".$this->input->post('order');
					break;
				case "date_added":
					$sortone = "u.date_added ".$this->input->post('order');
					break;
				
				
				default:
					$sortone = "u.user_id ".$this->input->post('order');
					
			}
			
				
			
		$arrayone = array();
		$keyarray = array();	
			
		
		$offset = $rows*($page-1);
		$num = $rows;
		
		$language_id = $this->Model_Config->getLanguageID($this->Model_Config->getKey('config_admin_language'));
		//$language_id = $this->Model_Config->getLanguageID('en');
		
		$results = $this->Model_User->get_specific_all($rows, $offset, $sortone, $keyarray, $language_id);
		
		foreach($results as $result)
		{
			$array_one[] = array(
				'user_id'    => $result['user_id'],
				'username'   => $result['username'],
				'status'     => ($result['status'] ? $this->lang->line('text_enabled') : $this->lang->line('text_disabled')),
				'date_added' => date($this->lang->line('date_format_short'), strtotime($result['date_added']))
			);
		}
		
		$total_num = $this->Model_User->get_specific_count_all($keyarray);
		
		
		$array = array(
			"total" => $total_num,
			"rows" => $array_one,
		);
		
		echo json_encode($array);
	}
   
  	public function insert() {
    	$this->load->language('user/user');

    	$this->document->setTitle($this->lang->line('heading_title'));
		
		$this->load->model('user/Model_User');
		
    	if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validateForm()) {
			$this->Model_User->addUser($this->input->post());
			
			$this->session->set_userdata('success', $this->lang->line('text_success'));
						
			redirect('user/user');
    	}
	
    	$this->getForm();
  	}

  	public function update() {
    	$this->load->language('user/user');

    	$this->document->setTitle($this->lang->line('heading_title'));
		
		$this->load->model('user/Model_User');
		
    	if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validateForm()) {
			$this->Model_User->editUser($this->uri->segment(4), $this->input->post());
			
			$this->session->set_userdata('success', $this->lang->line('text_success'));
			
			redirect('user/user');
    	}
	
    	$this->getForm();
  	}
 
  	public function delete() { 
		$success = false;
    	$this->load->language('user/user');
		$this->load->model('user/Model_User');
		
		$selected = explode('-', $this->input->post('selected'));
		
		
		if ($selected && $this->validateDelete()) {
			foreach ($selected as $user_id)
			{
				$success = $this->Model_User->deleteUser($user_id);
			}
			
			if($success)
			{
				echo "succ";
			}
			else
			{
				echo "fail1";
			}
		}
		else
		{
			echo "fail2";
		}
  	}

  	private function getList() {					
  		$data['breadcrumbs'] = array();

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('text_home'),
			'href'      => site_url('common/home'),
      		'separator' => false
   		);

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('heading_title'),
			'href'      => site_url('user/user'),
      		'separator' => ' :: '
   		);
			
		$data['insert'] = site_url('user/user/insert');
		$data['delete'] = site_url('user/user/delete');	
		$data['edit'] = site_url('user/user/update');	

		$data['datagrid'] = site_url('user/user/datagrid');
			
		$data['heading_title'] = $this->lang->line('heading_title');
		
		$data['text_no_results'] = $this->lang->line('text_no_results');

		$data['column_username'] = $this->lang->line('column_username');
		$data['column_status'] = $this->lang->line('column_status');
		$data['column_date_added'] = $this->lang->line('column_date_added');
		$data['column_action'] = $this->lang->line('column_action');
		
		$data['message_selections'] = $this->lang->line('message_selections');
		
		$data['button_insert'] = $this->lang->line('button_insert');
		$data['button_delete'] = $this->lang->line('button_delete');
		$data['button_edit'] = $this->lang->line('button_edit');
 
 		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}
		
		if ($this->session->userdata('success')) {
			$data['success'] = $this->session->userdata('success');
		
			$this->session->unset_userdata('success');
		} else {
			$data['success'] = '';
		}
		
		
		$data['header'] = helper_load_header();
		$data['footer'] = helper_load_footer();
		
		$this->load->view('user/user_list', $data);
  	}
	
	private function getForm() {
    	$data['heading_title'] = $this->lang->line('heading_title');

    	$data['text_enabled'] = $this->lang->line('text_enabled');
    	$data['text_disabled'] = $this->lang->line('text_disabled');
		
    	$data['entry_username'] = $this->lang->line('entry_username');
    	$data['entry_password'] = $this->lang->line('entry_password');
    	$data['entry_confirm'] = $this->lang->line('entry_confirm');
    	$data['entry_firstname'] = $this->lang->line('entry_firstname');
    	$data['entry_lastname'] = $this->lang->line('entry_lastname');
    	$data['entry_email'] = $this->lang->line('entry_email');
    	$data['entry_user_group'] = $this->lang->line('entry_user_group');
		$data['entry_status'] = $this->lang->line('entry_status');
		$data['entry_captcha'] = $this->lang->line('entry_captcha');

    	$data['button_save'] = $this->lang->line('button_save');
    	$data['button_cancel'] = $this->lang->line('button_cancel');

    	$data['tab_general'] = $this->lang->line('tab_general');
    
 		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

 		if (isset($this->error['username'])) {
			$data['error_username'] = $this->error['username'];
		} else {
			$data['error_username'] = '';
		}

 		if (isset($this->error['password'])) {
			$data['error_password'] = $this->error['password'];
		} else {
			$data['error_password'] = '';
		}
		
 		if (isset($this->error['confirm'])) {
			$data['error_confirm'] = $this->error['confirm'];
		} else {
			$data['error_confirm'] = '';
		}
		
	 	if (isset($this->error['firstname'])) {
			$data['error_firstname'] = $this->error['firstname'];
		} else {
			$data['error_firstname'] = '';
		}
		
	 	if (isset($this->error['lastname'])) {
			$data['error_lastname'] = $this->error['lastname'];
		} else {
			$data['error_lastname'] = '';
		}

  		$data['breadcrumbs'] = array();

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('text_home'),
			'href'      => site_url('common/home'),
      		'separator' => false
   		);

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('heading_title'),
			'href'      => site_url('user/user'),
      		'separator' => ' :: '
   		);
		
		if ((int)$this->uri->segment(4) == 0) {
			$data['action'] = site_url('user/user/insert');
		} else {
			$data['action'] = site_url('user/user/update/' . (int)$this->uri->segment(4));
		}
		  
    	$data['cancel'] = site_url('user/user');

    	if (((int)$this->uri->segment(4) != 0) && ($this->input->server('REQUEST_METHOD') != 'POST')) {
      		$user_info = $this->Model_User->getUser((int)$this->uri->segment(4));
    	}

    	if (false !== $this->input->post('username')) {
      		$data['username'] = $this->input->post('username');
    	} elseif (isset($user_info)) {
			$data['username'] = $user_info['username'];
		} else {
      		$data['username'] = '';
    	}
  
  		if (false !== $this->input->post('password')) {
    		$data['password'] = $this->input->post('password');
		} else {
			$data['password'] = '';
		}
		
  		if (false !== $this->input->post('confirm')) {
    		$data['confirm'] = $this->input->post('confirm');
		} else {
			$data['confirm'] = '';
		}
  
    	if (false !== $this->input->post('firstname')) {
      		$data['firstname'] = $this->input->post('firstname');
    	} elseif (isset($user_info)) {
			$data['firstname'] = $user_info['firstname'];
		} else {
      		$data['firstname'] = '';
    	}

    	if (false !== $this->input->post('lastname')) {
      		$data['lastname'] = $this->input->post('lastname');
    	} elseif (isset($user_info)) {
			$data['lastname'] = $user_info['lastname'];
		} else {
      		$data['lastname'] = '';
   		}
  
    	if (false !== $this->input->post('email')) {
      		$data['email'] = $this->input->post('email');
    	} elseif (isset($user_info)) {
			$data['email'] = $user_info['email'];
		} else {
      		$data['email'] = '';
    	}

    	if (false !== $this->input->post('user_group_id')) {
      		$data['user_group_id'] = $this->input->post('user_group_id');
    	} elseif (isset($user_info)) {
			$data['user_group_id'] = $user_info['user_group_id'];
		} else {
      		$data['user_group_id'] = '';
    	}
		
		$this->load->model('user/Model_User_Group');
		
    	$data['user_groups'] = $this->Model_User_Group->get_specific_all();
 
     	if (false !== $this->input->post('status')) {
      		$data['status'] = $this->input->post('status');
    	} elseif (isset($user_info)) {
			$data['status'] = $user_info['status'];
		} else {
      		$data['status'] = 0;
    	}
		
		$data['header'] = helper_load_header();
		$data['footer'] = helper_load_footer();
						
		$this->load->view('user/user_form', $data);	
  	}
  	
  	private function validateForm() {
    	if (!$this->Model_User->has_permission('user_edit', $this->session->userdata('user_id'))) {
      		$this->error['warning'] = $this->lang->line('error_permission');
    	}
    
    	if ((strlen(utf8_decode($this->input->post('username'))) < 3) || (strlen(utf8_decode($this->input->post('username'))) > 20)) {
      		$this->error['username'] = $this->lang->line('error_username');
    	}

    	if ((strlen(utf8_decode($this->input->post('firstname'))) < 1) || (strlen(utf8_decode($this->input->post('firstname'))) > 32)) {
      		$this->error['firstname'] = $this->lang->line('error_firstname');
    	}

    	if ((strlen(utf8_decode($this->input->post('lastname'))) < 1) || (strlen(utf8_decode($this->input->post('lastname'))) > 32)) {
      		$this->error['lastname'] = $this->lang->line('error_lastname');
    	}

    	if (($this->input->post('password')) || ((int)$this->uri->segment(4) == 0)) {
      		if ((strlen(utf8_decode($this->input->post('password'))) < 4) || (strlen(utf8_decode($this->input->post('password'))) > 20)) {
        		$this->error['password'] = $this->lang->line('error_password');
      		}
	
	  		if ($this->input->post('password') != $this->input->post('confirm')) {
	    		$this->error['confirm'] = $this->lang->line('error_confirm');
	  		}
    	}
	
    	if (!$this->error) {
      		return true;
    	} else {
      		return false;
    	}
  	}

  	private function validateDelete() { 
    	if (!$this->Model_User->has_permission('user_delete', $this->session->userdata('user_id'))) {
      		$this->error['warning'] = $this->lang->line('error_permission');
    	} 
	  	
		$selected = explode('-', $this->input->post('selected'));
		foreach ($selected as $user_id) {
			if ($this->session->userdata('user_id') == $user_id) {
				$this->error['warning'] = $this->lang->line('error_account');
			}
		}
		 
		if (!$this->error) {
	  		return true;
		} else { 
	  		return false;
		}
  	}
}
?>